package com.web.cms.doc.service;

import com.blue.jdbc.JdbcDataSource;
import com.blue.jdbc.Page;
import com.web.cms.doc.dao.WebDao;
import com.web.cms.doc.model.Web;
import com.web.core.util.Const;
import com.web.core.util.WebException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("cmsWebService")
@Transactional
@JdbcDataSource(Const.DS_CMS)
public class WebService
{
	@Autowired
	private WebDao webDao;
	
	public WebService()
	{
	}
	
	public Web save(Web web)
	{
		if (webDao.exist(web, "code"))
			throw new WebException("网站标识【code=" + web.getCode() + "】已存在");
		
		if (web.getId() == null)
		{
			webDao.save(web);
		}
		else
		{
			webDao.update(web);
		}
		Web w = webDao.get(web.getId());
		return w;
	}
	
	public Web get(Integer id)
	{
		Web web = webDao.get(id);
		return web;
	}

	public Web getByCode(String code)
	{
		Web web = webDao.getObject("code", code);
		return web;
	}
	
	public Page<Web> list(Web web, Page<Web> page)
	{
		page = webDao.list(web, page);
		return page;
	}
	
	public List<Web> list(Web web)
	{
		List<Web> webList = webDao.list(web);
		return webList;
	}
	
}
